/**
** 锚点插件 插件
**author: ericever
**create time: 2015-7-24
**/
;(function($, window, document, undefined){
    'use strict';
    var old = $.fn.Anchor;
    var defaults = {
        list:[]
    };
    var Anchor = function (element, options) {
        var self = this;
        this.$element = $(element);
        this.config = $.extend(true,{}, defaults, options);
        var list = this.config.list;
        function build(){
            var the = $(element).empty();
            //if(the.html()!="") return;
            var list = $(".showcase-box>[id][data-anchor]");
            var vbar = $('<div class="nav nav-graphic">');
            var lis = $('<ul class="nav nav-anchor-wrapper">');
            var btn = $('<a class="btn btn-outline" name="app"><i class="glyphicon glyphicon-menu-up"></i></a>');
            vbar.append('<i class="glyphicon glyphicon-record itop"></i>');
            list.each(function(index,item){
                var id = $(this).attr("id");
                var tag = $(this).data("anchor");
                vbar.append('<i class="anchor" data-target='+id+'></i>');
                var li = $('<li><a name='+id+'>'+tag+'</a></li>');//href="#'+id+'"
                $(this).find("[id][data-anchor]").each(function(){
                    id = $(this).attr("id");
                    tag = $(this).data("anchor");
                    vbar.append('<i class="anchor" data-target='+id+'></i>');
                    if(li.find("ul").length==0) li.append('<ul class="nav nav-anchor-wrapper">');
                    li.find("ul").append('<li><a name='+id+'>'+tag+'</a></li>');
                });
                lis.append(li);
            });
            vbar.append('<i class="glyphicon glyphicon-record ibottom"></i>');
            vbar.append('<span class="gcursor"><i class="triangle-right"></i></span>');
            the.append(vbar).append(lis).append(btn);
            the.find("i.anchor").each(function(index,item){
                var id = $(item).data("target");
                $(item).css("top",$(item).parent().siblings("ul").find("li>a[name="+id+"]").offset().top-229);
            });

            function setPos(id){
                var ypox = $("i.anchor[data-target="+id+"]").css("top");
                if(ypox){
                    ypox = ypox.replace('px','')*1-5;
                    console.log(ypox);
                    $("div.nav-graphic").find("span.gcursor").css("top",ypox+'px');
                }
            }

            function idTop(id){
                return $("#"+id).length == 0 ? 0 : $("#"+id).position().top;
            }

            $('.fmp-article').scroll(function(e){//调整组件的位置
                //the.css("top",($('.fmp-article').scrollTop()+$(window).height()/2 -140) +"px");
                var y = $('.fmp-article').scrollTop();
                if(y<=100){
                    setPos("app");
                }else if(y>400 && y<(idTop("Location")-20)){
                    setPos("aim");
                //}else if(y>=idTop("Location")-20 && y<=(idTop("Location")+$("#Location").height())){
                //    setPos("Location");
                }else if(y<=idTop("Interests")+$("#Interests").height() && y>=(idTop("Interests")-30)){
                    setPos("Interests");
                }else if(y<=idTop("gen")+$("#gen").height() && y>=(idTop("gen")-30)){
                    setPos("gen");
                }else if(y>idTop("gen")+$("#gen").height() && y<(idTop("innovation")-50)){
                    setPos("price");
                }else if(y>idTop("innovation")+30 && y<(idTop("naming")-50)){
                    setPos("innovation");
                }else if(y>idTop("naming")-50){
                    setPos("naming");
                }
            });
            $("ul.nav-anchor-wrapper>li>a[name]").unbind("click").click(function(e){

                var name = $(this).attr("name");
                var $liDom =  $(this),$allLi = $("ul.nav-anchor-wrapper>li>a[name]");
                $('.fmp-article').animate({scrollTop:($("#"+name).position().top)+"px"},
                               {duration: 500,easing: "swing",complete:function(){
                                   var y = $("i.anchor[data-target="+name+"]").css("top");
                                   $("div.nav-graphic").find("span.gcursor").css("top",y);
                                   $allLi.css('color','#666666');
                                   $liDom.css('color','#fd9611');
                                   if(name=='price'){
                                    setTimeout(function() {
                                        setPos("price");
                                    },200)
                                   }
                               }
                });
            });

            btn.unbind("click").click(function(e){
                $("html,body").animate({scrollTop:"0px"},{duration: 500,easing: "swing"});
            });

            $(window).resize(function(){//调整组件的位置
                //the.css("top",($(document).scrollTop()+$(window).height()/2 -140) +"px");

            });

        };

        function destory(){
            $(element).empty();
        };

        this.startup = function(){
            build();
        }
    };

    /***
    ** 入口
    **/
    $.fn.anchor = function(options){
        var bd = new Anchor(this,options);
        bd.startup();
        return this;
    }
}(window.j$ || jQuery, window, document));
